<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Mobile_Order_module extends CI_Module
{
    public $data;
    public $sid ;
    private $table_order_hotel_goods;
    private $table_hotel_goods;
    private $order_status_array;
    private $pay_status_array;

    public function __construct()
    {
        parent::__construct();
        $this->sid = $GLOBALS['sid'];
        $this->data =$GLOBALS['config'];
        $this->load->model('order.Orderlog_model');
        $this->load->model('order.Orderpaylog_model');
        $this->load->model('order.Ordercontacts_model');
        $this->load->model('order.Orderinfo_model');
        $this->load->model('order.Orderhotel_model');
        $this->load->model('order.Orderhotelgoods_model');
        $this->load->model('member.User_model');
        $this->table_order_hotel_goods = $this->db->dbprefix("order_hotel_goods");
        $this->table_hotel_goods = $this->db->dbprefix("hotel_goods");


        $this->config->load('base');

        $this->order_status_array = $this->config->item('order_status');
        $this->pay_status_array = $this->config->item('pay_status');


    }


    /**
     * 酒店订单列表
     */
    public function hotel_list()
    {

        $user_id = $this->session->userdata('user_id');
        $this->session->set_userdata('redirect_uri',site_url('mobile/order/hotel_list'));
        if(!$user_id) redirect('mobile/oauth/oauth');

        $type = $this->input->get_post('type') ? $this->input->get_post('type') : 0;


        $order_info_table = $this->db->dbprefix("order_info");
        $pagesize = 3;
        $page = !empty($_GET['page']) && $_GET['page'] > 0 ? intval($_GET['page']) : 1;

        $where = $order_info_table . ".order_type=1  AND $order_info_table.user_id='" . $user_id . "'";//门票订单

        $title = '全部订单';
        if ($type == 1)
        {
            $where .=  ' and '.$order_info_table . '.order_status=4';
            $title = '已完成订单';
        }

        if ($type == 2)
        {
            $where .=  ' and '.$order_info_table . '.order_status !=4';
            $title = '未完成订单';
        }

        $result = $this->Orderinfo_model->get_index_hotel_list($pagesize, $page, $where);


        $this->data['datalist'] = $result;
        $this->data['type'] = $type;

        foreach ($this->data['datalist'] as $k => $v)
        {
            $this->data['datalist'][$k]['link'] = site_url('mobile/order/hotel_profile/?id=' . $v['order_id']);
        }
 
        $this->data['page'] = $page;
        $this->data['common'] = array(
            array(
                'head'   => $title,
                'nav'   => 'center',
                'title'   => $title,
                'noshare'    => '1'
            )
        );
        $this->data['index_url'] = site_url('mobile/order/hotel_list');

        if (!empty($_GET['isajax']))
        {
            header('Content-type:application/json;charset=utf-8');
            echo json_encode($this->data['datalist']);
            exit;
        }
        else
        {
            $this->load->view(MOBILE_TEMPLATE_NAME . '/' . 'hotel_order_list', $this->data);
        }
    }


    /**
     * 酒店订单详情
     */
    public function hotel_profile()
    {
        
        @$user_id = $this->session->userdata('user_id');
        $this->data['user_id'] = $user_id;

        $id = intval($_GET['id']);
        $info = $this->order_info($id);
        $this->data['info'] = $info ;

        if(isset($info['order_status']))
        {
            $this->data['info']['order_status_name'] = $this->order_status_array[$info['order_status']];
        }
        if(isset($info['pay_status']))
        {
            $this->data['info']['pay_status_name'] = $this->pay_status_array[$info['pay_status']];
        }


        $this->data['info']['pay_identify'] = false ;
        $this->data['info']['cancel_identify'] = false ;

        // 订单未支付完成 订单状态为已审核状态
        if($info['pay_status']!='3' && $info['order_status']=='2')
        {
            $this->data['pay']['pay_identify'] = true ;
            $this->data['pay']['pay_title'] = '支付定金' ;
        }

        if($info['pay_status']!= 3 && $info['order_status']== 5 )
        {
            $this->data['pay']['pay_identify'] = true ;
            $this->data['pay']['pay_title'] = '支付尾款' ;
        }



        if(in_array($info['order_status'], array(1,2,4)))
        {
            $this->data['info']['cancel_identify'] = true ;
        }


        $field = format_table_field(array('id','number'),$this->table_order_hotel_goods);

        $field .= ','.format_table_field(array('name','money'),$this->table_hotel_goods);



        $goods_list = $this->Orderhotelgoods_model->get_all_list("$this->table_order_hotel_goods.order_id = $id",$field);

       if($goods_list)
       {
           foreach ($goods_list as $key => $value)
           {
               $goods_list[$key]['total'] = $value['money'] * $value['number'] ;
           }
       }

        $this->data['info']['pay_url'] = site_url('mobile/pay/index/'.$id) ; ;
        $this->data['info']['check_pay_url'] = site_url('mobile/pay/check_pay') ; ;
        $this->data['info']['cancel_url'] = site_url('mobile/order/order_status') ;
        $this->data['info']['title_name']= '酒店' ;
        $this->data['info']['order_id']= $id ;
        $this->data['goods_list']= $goods_list ;


        $this->data['common'] = array(
            array(
                'head'   =>  $this->data['info']['order_title'],
                'noshare'    => '1',
                'title'   =>  $this->data['info']['order_title'].'订单',
                'keywords'   => '',
                'nav'   => 'center',
                'description'   => ''
            )
        );


        $this->load->view(MOBILE_TEMPLATE_NAME.'/'.'order_hotel_profile',$this->data);
    }








    public function order_status()
    {
        if(strtolower($_SERVER['REQUEST_METHOD']) == 'post')
        {
            $order_id = $this->input->get_post('order_id');
            $data = array('order_id'=>$order_id,'order_status'=>3,'user_type'=>2);
            $res = $this->Orderinfo_model->translate_order_status($data);
            if($res)
            {

                $order_info = $this->order_info($order_id);
                if (!intval($order_info['earnest_paid_account']))
                {
                    // 通知游客
                    $replace = array('sname' => $this->data['company'], 'date' => $order_info['start_date'], 'phone' => $this->data['notify_num']);
                    $this->User_model->_send_sms($order_info['mobile'], 'SMS_10870431', $replace);


                    // 通知管理员
                    $replace = array('name' => $order_info['name'], 'date' => $order_info['start_date'], 'phone' => $order_info['mobile']);
                    $this->User_model->_send_sms($this->data['notify_num'], 'SMS_10825622', $replace);
                }



                return json_encode(array('status'=>'1','info' =>'取消成功'));
            }
            return json_encode(array('status'=>'0','info' =>'取消失败'));
        }
    }



    public function order_info($order_id)
    {
        $return = array() ;
        $order_info = $this->Orderinfo_model->get_row(array('order_id'=>$order_id));
        $contacts_info = $this->Ordercontacts_model->get_row(array('order_id'=>$order_id));

        if($order_info['order_type']=='1')
        {
            $table = $this->db->dbprefix("order_hotel");
            $order_detail = $this->Orderhotel_model->get_row(array($table.'.order_id'=>$order_id));
            $return = $order_detail;
            $return['order_sn'] = $order_info['order_sn'];
            $return['order_title'] = '农家乐酒店:'.$order_detail['number'].'人';
            $return['start_date'] =  $order_detail['start_date'];
            $return['end_date'] =  $order_detail['end_date'];
            $return['order_num'] =  $order_detail['number'].'人('.$order_detail['male_number'].'男'.$order_detail['woman_number'].'女)';

            $return['order_type'] = $order_info['order_type'] ;
            $return['cat_type'] = $order_info['cat_type'] ;
            $return['order_status'] = $order_info['order_status'] ;
            $return['mobile'] = $contacts_info['phone_num'] ;
            $return['name'] = $contacts_info['name'] ;
            $return['pay_status'] = $order_info['pay_status'] ;
            $return['order_account'] = $order_info['order_account'] ;
            $return['paid_account'] = $order_info['paid_account'] ;
            $return['earnest_account'] = $order_info['earnest_account'] ;
            $return['earnest_paid_account'] = $order_info['earnest_paid_account'] ;
            $return['redirect'] = site_url('mobile/order/hotel_profile/?id='.$order_id);
            $return['goods_url'] = site_url("jingdian/{$order_detail['scenic']}.html");
            $return['add_time'] = $order_info['add_time'];

        }

        return $return ;
    }


}